import _ from 'lodash'

export const util={
    when(startFrom,hours){
        return `${String(startFrom).substring(0, 2)}~${(Number(String(startFrom).substring(0, 2))+1)}学年第${String(startFrom).substring(2, 4)}学期第${String(startFrom).substring(4, 6)}周星期${String(startFrom).substring(6, 7)}第${String(startFrom).substring(7, 9)}节共${hours}学时`;
    },

/**
 * 将后端返回的数组形式转换成js对象形式,原形式为{[id,state,curriculumId,teacherId,teacherName, courseId, courseName ,clazzId,clazzName,startFrom,hours,detailId,studentId,studentName,studentState]}
 * 转换后的对象为{id,state,curriculumId,teacherId,teacherName, courseId, courseName ,clazzId,clazzName,startFrom,hours,detail:[id,studentId,studentName,state]} 
 * 注意：自己必须确保输入的数组里只有一个考勤表信息
* @param {*} attendWithDetailArray 
 */
    convertAttendDetailArrayToAttendObject(attendWithDetailArray){
        return _.reduce(attendWithDetailArray,(result,item)=>{
            result.id=item.id;
            result.curriculumId=item.curriculumId;
            result.teacherId=item.teacherId;
            result.teacherName=item.teacherName;
            result.courseId=item.courseId;
            result.courseName=item.courseName;
            result.clazzId=item.clazzId;
            result.clazzName=item.clazzName;
            result.startFrom=item.startFrom;
            result.hours=item.hours;
            result.state=item.state;
            result.detail.push({id:item.detailId,studentId:item.studentId,studentName:item.studentName,state:item.studentState})
            return result;
        },{detail:[]});
    },

    /**
     * 将课表条目curriculum对象文字化。
     * @param {*} curriculum 必须包含startFrom,hours,courseName,teacherName属性的curriculum对象
     * @returns 
     */
    curriculumAlias(curriculum){
        return this.when(curriculum.startFrom,curriculum.hours)+"-"+curriculum.courseName+"-"+curriculum.teacherName;
    }

};


